package com.netbao.fin.service.impl;


import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.netbao.fin.mapper.SubtotalMapper;
import com.netbao.fin.mapper.TotalMapper;
import com.netbao.fin.model.Subtotal;
import com.netbao.fin.model.Total;
import com.netbao.fin.service.QingService;
import com.netbao.fin.vomapper.QingVoMapper;
import com.netbao.model.PageBean;
import com.netbao.model.PageResult;
import com.netbao.model.Result;
import com.netbao.sys.utils.ShiroUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

/**
 * @author suqk
 * @create 2019-11-05 15:12
 */
@Service
public class QingServiceImpl implements QingService {
    @Resource
    private QingVoMapper qingVoMapper;
    @Resource
    private SubtotalMapper subtotalMapper;
    @Resource
    private TotalMapper totalMapper;


    @Override
    public PageResult listAll(PageBean pageBean, String name) {
        Integer eid= ShiroUtils.getUserEntity().getEid();
        PageResult pageResult = new PageResult();
        if (!StringUtils.isEmpty(name)) {
            PageHelper.startPage(pageBean.getPage(), pageBean.getRows());
            PageInfo<Subtotal> userPageInfo = new PageInfo<Subtotal>(qingVoMapper.mo(pageBean,name, (long) eid));
            return new PageResult(userPageInfo.getTotal(), userPageInfo.getList());
        }
        PageHelper.startPage(pageBean.getPage(), pageBean.getRows());
        PageInfo<Subtotal> userPageInfo = new PageInfo<Subtotal>(qingVoMapper.listAll(pageBean,(long) eid));
        return new PageResult(userPageInfo.getTotal(), userPageInfo.getList());
    }

    @Override
    public Result saveup(Long id) {
        Subtotal subtotal = new Subtotal();
        subtotal.setId(id);
        subtotal.setCstatus((long) 1);
        subtotalMapper.updateByPrimaryKeySelective(subtotal);
        try {
            insert(id);
        } catch (ParseException e) {
            e.printStackTrace();
        }
        return Result.ok();
    }

    @Override
    public Result exit(Long id) {
        Subtotal subtotal = new Subtotal();
        subtotal.setId(id);
        subtotal.setCstatus((long) 4);
        subtotalMapper.updateByPrimaryKeySelective(subtotal);
        qingVoMapper.exit(id);
        return Result.ok();
    }

    @Override
    public List<Subtotal> list(Long id) {
        Integer eid= ShiroUtils.getUserEntity().getEid();
        return qingVoMapper.list(id,(long)eid);
    }

    public Result insert(Long id) throws ParseException {
        Integer eid= ShiroUtils.getUserEntity().getEid();
        List<Subtotal> list = qingVoMapper.list(id,(long)eid);
        Total total = new Total();
        total.setApplicant(list.get(0).getCaptain().getCname());
        total.setPhone(list.get(0).getCaptain().getPhone());
        total.setPayment(list.get(0).getPayment());
        total.setCtotal(list.get(0).getCtotal());
        total.setEid((long)eid);
        Date date=null;
        SimpleDateFormat dateFormat= new SimpleDateFormat("yyyy-MM-dd");
        date=dateFormat.parse(dateFormat.format(new Date()));
        total.setLastime(date);
        total.setOpid(list.get(0).getId());
        total.setCstatus(list.get(0).getCstatus());
        totalMapper.insertSelective(total);
        return Result.ok();
    }
}
